import math
import random
import time
import datetime
import Source.precondition as pre
from functools import reduce


def inputAndOutput():
    """
    1
    :return:
    """
    a = eval(input("请输入正整数a："))

    pre.checkType(a, int, "please input positive integer!")
    pre.checkValue(a > 0, "please input positive integer!")

    b = eval(input("请输入正整数b："))
    pre.checkType(b, int, "please input positive integer!")
    pre.checkValue(b > 0, "please input positive integer!")

    print("a//b={0} a%b={1}".format(a // b, a % b))


def inputAndOutputNameLength():
    """
    2
    :return:
    """
    name = input("请输入你的名字：")
    print(len(name))


def getWaterBucketCount():
    """
    3
    :return:
    """
    r = eval(input("请输入半径(cm)："))
    h = eval(input("请输入高度(cm)："))
    print("至少需要喝{0:.0f}桶水".format(math.ceil(20000 / (math.pi * r * r * h))))


def calDistance():
    """
    4
    :return:
    """
    x1, y1 = map(eval, input("请输入坐标1（横纵坐标以逗号分隔）:").split(','))
    x2, y2 = map(eval, input("请输入坐标2（横纵坐标以逗号分隔）:").split(','))
    print(math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2))


def reverseNumber():
    """
    5
    :return:
    """
    n = random.randint(100,999)
    print(n)
    strNum = "".join(reversed(str(n))).lstrip("0")
    print(strNum)


def calSum():
    """
    6
    :return:
    """
    n = eval(input("请输入一个三位整数："))
    pre.checkValue(type(n) == int and 99 <= abs(n) <= 999, "the num you input must be three-digit integer")
    print("{:>4}".format(sum(int(c) for c in str(n).lstrip("-"))))


def calTriangleArea():
    """
    7
    :return:
    """
    x1, y1 = map(eval, input("请输入第一个顶点（横纵坐标以逗号分隔）:").split(","))
    x2, y2 = map(eval, input("请输入第二个顶点（横纵坐标以逗号分隔）:").split(","))
    x3, y3 = map(eval, input("请输入第三个顶点（横纵坐标以逗号分隔）:").split(","))

    pre.checkValue((y3 - y1) * (x2 - x1) - (y2 - y1) * (x3 - x1) != 0, "These 3 points cannot be collinear")

    def calLength(_x1, _x2, _y1, _y2):
        return math.sqrt((_x1 - _x2)**2 + (_y1 - _y2)**2)

    sides = [calLength(x1, x2, y1, y2),
             calLength(x1, x3, y1, y3),
             calLength(x2, x3, y2, y3)]
    s = sum(sides) / 2
    area = math.sqrt(s * reduce(lambda x, y: x * y, (abs(s - side) for side in sides)))
    print("{:<7.2f}".format(area))


def calInterestRate():
    """
    8
    :return:
    """
    total = 0
    rate = 0.005
    for i in range(5):
        total = (total + 100) * (1 + rate)
    print("{:>.5f}".format(total))
    print("{:.2f}%".format((total - 500) / 500 * 100))


def printTime():
    """
    9
    :return:
    """
    print(time.strftime("%H: %M: %S", time.localtime(time.time())))


def randomSphere():
    """
    10
    :return:
    """
    r = random.uniform(5, 20)
    v = 4/3 * math.pi * (r ** 3)
    print("r={0:>15.3f}".format(r))
    print("v={0:>15.3f}".format(v))


def randomComplex():
    """
    11
    :return:
    """
    i = random.uniform(10, 50)
    j = random.uniform(10, 50)
    mod = math.sqrt(i**2 + j**2)
    degree = math.atan2(j,i) * 180 / math.pi
    print("complex = {0:>7.2f}+{1:>7.2f}j".format(i ,j))
    print("mod = {0:>7.2f}".format(mod))
    print("degree = {0:>7.2f}°".format(degree))


def diffTime():
    """
    12
    :return:
    """
    d1 = datetime.datetime(1970, 1, 1)
    d2 = datetime.datetime.now()
    print((d2 - d1).days, (d2 - datetime.datetime(2020, 3, 6)).total_seconds() / 3600)